package com.hk.wms.wmssentinelservice8401.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hk.wms.wmssentinelservice8401.myhandler.CustomerBlockHandler;
import com.hk.wms.wmssentinelservice8401.service.FeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@RestController
@Slf4j
public class FlowLimitController {
    @Resource
    FeignService feignService;
    @Value("${service-url.nacos-user-service}")
    String serverUrl ;
    @Value("${config-url.config-service}")
    String configUrl;
    @Autowired
    RestTemplate template;
    @GetMapping("/testA")
    @SentinelResource(value="testA",blockHandler = "deal_testA")
    public String testA(@RequestParam(value="p1",required = false)String p1) {
        System.out.println(Thread.currentThread().getName()+".................testA");
        return "----testA";
    }

    @GetMapping("/testD")
    public String testD() {
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        int i= 10/0;
        System.out.println("testD 测试RT");
        return "----testD";
    }
    @GetMapping("/testE")
    public String testE() {
        System.out.println("testE 测试异常数");
        int age = 10 / 0;
        return "----testE 测试异常数";
    }



    @GetMapping("/testB")
    @SentinelResource(value="testB")
    public String testB() {
        try {
            Thread.sleep(800L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            return "----testA";

        }


    }
    @GetMapping("/testC")
    public String testC() {
        return template.getForObject(serverUrl+"/provider/",String.class);
    }
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false)String p2) {
        return "----testHotKey";
    }
    @GetMapping("/customBlockHandler")
    @SentinelResource(value="customBlockHandler",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "customBlockHandler")
    public String customBlockHandler(){
        return "测试成功";
    }



    public String deal_testHotKey(String p1, String p2, BlockException exception) {
        return "----deal_testHotKey, o(╥﹏╥)o"; // sentinel的默认提示都是： Blocked by Sentinel (flow limiting)
    }
    public String deal_testA(String p1,BlockException e){
        return "----testA+";
    }

    @GetMapping("/testE/{id}")
    @SentinelResource(value="testE",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "customBlockHandler2",fallback = "handlerFallBack")
    public String getStrController(@PathVariable("id")int id){
        String str=feignService.getString(id);
        if(id<0){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常...");
        }
        if(str==null){
            throw new NullPointerException("NullPointerException,该ID没有对应记录，空指针异常");
        }
        return str;
    }
    @GetMapping("/testE2/{id}")
    @SentinelResource(value="testE",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "customBlockHandler2",fallback = "handlerFallBack")
    public String getStrController2(@PathVariable("id")int id){
        String str=feignService.getString2(id);
        if(id<0){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常...");
        }
        if(str==null){
            throw new NullPointerException("NullPointerException,该ID没有对应记录，空指针异常");
        }
        return str;
    }

    @GetMapping("/testF/{id}")
    @SentinelResource(value="testF",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "customBlockHandler1",fallback = "handlerFallBack")
   public String CircleBreakerController(@PathVariable("id")int id){
        String str=template.getForObject(serverUrl+"/getStr/"+id,String.class);
        if(id<0){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常...");
        }
        if(str==null){
            throw new NullPointerException("NullPointerException,该ID没有对应记录，空指针异常");
        }
        return str;
   }
   public String handlerFallBack(@PathVariable int id,Throwable e){
        return "异常";
   }

}

